package com.youlin.controller;

import com.youlin.dto.*;
import com.youlin.dto.req.TakerLogin;
import com.youlin.dto.res.*;
import com.youlin.service.BygCustomService;
import com.youlin.service.BygDeadManService;
import com.youlin.service.BygSignInService;
import com.youlin.service.BygUndertakerService;
import com.youlin.untils.MyBeanUtil;
import com.youlin.untils.TokenIntercept;
import com.youlin.untils.UnIntercept;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiOperationSort;
import io.swagger.annotations.ApiSort;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

@ApiSort(10)
@Api(tags = {"10 - 承办人查看签到模块"})
@RestController
@RequestMapping("/bygTaker")
public class BygTakerController {

    @Autowired
    private BygUndertakerService bygUndertakerService;
    @Autowired
    private BygDeadManService bygDeadManService;
    @Autowired
    private BygSignInService bygSignInService;
    @Autowired
    private BygCustomService bygCustomService;

    /**
     * 承办人登录发送手机验证码
     * @param baseReqDto
     * @return
     */
    @UnIntercept
    @PostMapping("/guest/sendCode")
    @ApiOperationSort(100)
    @ApiOperation(value = "100 - 承办人登录发送手机验证码", notes = "请求参数data = 承办人手机号\n" +
            "响应参数：status-message = {1023-发送验证码请求频繁}")
    public BaseResDto sendCode (@RequestBody BaseReqDto<String> baseReqDto) {
        String tel = baseReqDto.getData();
        if (StringUtils.isBlank(tel) || ! tel.matches("^[1][0-9]{10}$"))
            return new BaseResDto(Status.PARAMETERERROR);
        return bygUndertakerService.sendCode (tel);
    }

    /**
     * 承办人登录
     * @param baseReqDto
     * @return
     */
    @UnIntercept
    @PostMapping("/guest/takerLogin")
    @ApiOperationSort(200)
    @ApiOperation(value = "200 - 承办人登录", notes = "响应参数：data = takerToken\n" +
            "响应参数：status-message = {1018-登录验证码已失效, 1019-登录验证码错误}")
    public BaseDataResDto<String> takerLogin (@RequestBody BaseReqDto<TakerLogin> baseReqDto) {
        TakerLogin takerLogin = baseReqDto.getData();
        if (MyBeanUtil.isParamError(takerLogin)) return new BaseDataResDto(Status.PARAMETERERROR);
        return bygUndertakerService.takerLogin (takerLogin.getTel(), takerLogin.getCode());
    }

    /**
     * 承办人查看：获取逝者信息
     * @return
     */
    @TokenIntercept
    @PostMapping("/guest/findDmByAmUuId")
    @ApiOperationSort(300)
    @ApiOperation(value = "300 - 承办人查看：获取逝者信息", notes = "请求参数：data = 预约UUID")
    public BaseDataResDto<DmFinallyResDto> findDmByAmUuId (@RequestBody BaseReqDto<String> baseReqDto,
                                                   @RequestHeader("takerToken") String takerToken) {
        String amUuId = baseReqDto.getData();
        if (StringUtils.isBlank(amUuId)) return new BaseDataResDto(Status.PARAMETERERROR);
        return bygDeadManService.findDmByAmUuId (amUuId, takerToken);
    }

    /**
     * 承办人查看：获取来宾签名列表
     * @return
     */
    @TokenIntercept
    @PostMapping("/guest/findAllSignInNames")
    @ApiOperationSort(400)
    @ApiOperation(value = "400 - 承办人查看：获取来宾签名列表", notes = "请求参数：data = 预约UUID\n" +
            "响应参数：status-message = {1017-需要承办人登录后查看}")
    public PageDataResDto<List<SINameResDto>> findAllSignInNames (@RequestBody PageDataReqDto<String> baseReqDto,
                                                              @RequestHeader("takerToken") String takerToken) {
        String amUuId = baseReqDto.getData();
        PageInfo pageInfo = baseReqDto.getPageInfo();
        if (amUuId == null) return new PageDataResDto<>(Status.PARAMETERERROR);
        return bygSignInService.findAllSignIns (amUuId, 0, pageInfo, SINameResDto.class);
    }

    /**
     * 承办人查看：获取来宾祝福内容列表
     * @return
     */
    @TokenIntercept
    @PostMapping("/guest/findAllSignInContents")
    @ApiOperationSort(500)
    @ApiOperation(value = "500 - 承办人查看：获取来宾祝福内容列表", notes = "请求参数：data = 预约UUID\n" +
            "响应参数：status-message = {1017-需要承办人登录后查看}")
    public BaseResDto<List<SIContentResDto>> findAllSignInContents (@RequestBody PageDataReqDto<String> baseReqDto,
                                                                @RequestHeader("takerToken") String takerToken) {
        String amUuId = baseReqDto.getData();
        PageInfo pageInfo = baseReqDto.getPageInfo();
        if (amUuId == null) return new BaseResDto<>(Status.PARAMETERERROR);
        return bygSignInService.findAllSignIns (amUuId, 1, pageInfo, SIContentResDto.class);
    }

    /**
     * 承办人查看：获取风俗信息列表
     * @param baseReqDto
     * @return
     */
    @TokenIntercept
    @PostMapping("/guest/findAllCustoms")
    @ApiOperationSort(600)
    @ApiOperation(value = "600 - 承办人查看：获取风俗信息列表", notes = "请求参数：data = 预约UUID \n" +
            "响应参数：status-message = {1017-需要承办人登录后查看}")
    public PageDataResDto<List<CMListResDto>> findAllCustoms (@RequestBody PageDataReqDto<String> baseReqDto,
                                                @RequestHeader("takerToken") String takerToken) {
        String amUuId = baseReqDto.getData();
        if (StringUtils.isBlank(amUuId)) return new PageDataResDto<>(Status.PARAMETERERROR);
        return bygCustomService.findAllCustoms (amUuId, baseReqDto.getPageInfo());
    }

    /**
     * 承办人查看：获取风俗信息详情
     * @param baseReqDto
     * @return
     */
    @TokenIntercept
    @PostMapping("/guest/findCustomById")
    @ApiOperationSort(700)
    @ApiOperation(value = "700 - 承办人查看：获取风俗信息详情", notes = "请求参数：data = 风俗信息ID\n" +
            "响应参数：status-message = {1017-需要承办人登录后查看}")
    public BaseDataResDto<CustomResDto> findCustomById (@RequestBody BaseReqDto<Integer> baseReqDto,
                                                    @RequestHeader("takerToken") String takerToken) {
        Integer customId = baseReqDto.getData();
        if (customId == null) return new BaseDataResDto<>(Status.PARAMETERERROR);
        return bygCustomService.findCustomById (customId);
    }

}
